package com.emm.yixun.website.controller.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.Merchant;
import com.emm.yixun.common.model.Resources;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.system.service.MerchantServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;

//用户权限资源
@RestController
@RequestMapping("/common/permission/")
public class PermissionController extends BaseController<Customer>{
	@Autowired
	private MerchantServiceFacade merchantServiceFacade;
	
	@RequestMapping(value = "getButtons", method = RequestMethod.POST)
	public AjaxRes getButtons(String parentCode) {
		AjaxRes ar=getAjaxRes();
		List<Resources> buttonList=getRoleButtonResources(parentCode);
		List<String> permissionList=new ArrayList<String>();
		if(null!=buttonList){
			for(Resources resources : buttonList){
				permissionList.add(resources.getPermission());
			}
		}
		ar.setSucceed(permissionList);
		return ar;
	}
	
	@RequestMapping(value = "getToDist", method = RequestMethod.POST)
	public AjaxRes getToDist(String parentCode) {
		AjaxRes ar=getAjaxRes();
		ar.setSucceed(false);
		User user = ShiroUtil.getCurrentUser();
		long merchantId = user.getMerchantId();
		Response<Merchant> merchantRes = merchantServiceFacade.findById(merchantId);
		if(merchantRes.isSuccess()){
			Merchant merchant = merchantRes.getEntity();
			String phoneInformation = merchant.getPhoneInformation();
			JSONArray jsonArray = JSON.parseArray(phoneInformation);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
			    String type = jsonObject.getString("type");
			    String status = jsonObject.getString("status");
			    if("6".equals(type)&&"1".equals(status)){
			    	ar.setSucceed(true);
			    }
			}
		}
		return ar;
	}
}
